# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

::Chef::Recipe.send(:include, Opscode::OpenSSL::Password)

node.set_unless['lumis']['lumisportal']['database']['password'] = secure_password
password = node['lumis']['lumisportal']['database']['password']

bash "create_local_mysql_db" do
  cwd Chef::Config[:file_cache_path]
  code <<-EOH
  echo 'CREATE DATABASE IF NOT EXISTS lumisportal;
  GRANT USAGE ON lumisportal.* TO #{node['lumis']['lumisportal']['database']['username']}@localhost IDENTIFIED BY "#{password}";
  GRANT ALL PRIVILEGES ON lumisportal.* TO lumis@localhost;
  GRANT USAGE ON lumisportal.* TO #{node['lumis']['lumisportal']['database']['username']}@"%" IDENTIFIED BY "#{password}";
  GRANT ALL PRIVILEGES ON lumisportal.* TO lumis@"%";' > ./createdatabase.sql
  /usr/bin/mysql -u root --password=#{node['lumis']['mysql']['root_password']} < ./createdatabase.sql
  EOH
end
